//
//  CJFoundMainController.m
//  CoJellyCartoon
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJFoundMainController.h"
#import "CJFoundMainModel.h"
#import "HotWordTableViewCell.h"
#import "SearchViewController.h"
#import "CJRankControllerViewController.h"
@interface CJFoundMainController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) CJBaseTableView *table;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) CJFoundMainModel *foundModel;
@property (nonatomic, strong) CJBaseVIew *headView;
@property (nonatomic, strong) UIButton *quanqu;
@property (nonatomic, strong) UIButton *yuanchuang;

@end

@implementation CJFoundMainController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    
    [self creatHeadView];
    [self createTabelView];
    
    
    [self hotWordsRequest];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}
- (void)createTabelView {
    self.table = [[CJBaseTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 110) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    self.table.tableHeaderView = self.headView;
    
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.table registerClass:[HotWordTableViewCell class] forCellReuseIdentifier:@"HotWordTableViewCellIdentifier"];
    
    [self.view addSubview:self.table];
}

- (void)creatHeadView {
    self.headView = [[CJBaseVIew alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight * 140 / 736)];
    
    self.quanqu = [UIButton buttonWithType:UIButtonTypeCustom];
    self.quanqu.frame = CGRectMake(ScreenWidth * 10 / 414, 10, ScreenWidth * 192 / 414, ScreenHeight * 120 / 736);
    [self.quanqu setImage:[UIImage imageNamed:@"quanqu"] forState:UIControlStateNormal];
    [self.quanqu addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.quanqu];
    self.quanqu.tag = 1001;
    
    
    self.yuanchuang = [UIButton buttonWithType:UIButtonTypeCustom];
    self.yuanchuang.frame = CGRectMake(ScreenWidth * 212 / 414, 10, ScreenWidth * 192 / 414, ScreenHeight * 120 / 736);
    [self.yuanchuang setImage:[UIImage imageNamed:@"yuanchuang"] forState:UIControlStateNormal];
    [self.yuanchuang addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.yuanchuang];
    self.yuanchuang.tag = 1002;
   
    
}
- (void)buttonAction:(UIButton *)sender {
    CJRankControllerViewController *rankVC = [[CJRankControllerViewController alloc] init];

    rankVC.quanqu = self.quanqu.tag;
    //(@"====%ld", rankVC.quanqu);
//    rankVC.yuanchuang = self.yuanchuang.tag;
//    rankVC.yuanchuang = sender.tag;
    
    [self.navigationController pushViewController:rankVC animated:YES];
}

- (void)button2Action:(UIButton *)sender {
    CJRankControllerViewController *rankVC = [[CJRankControllerViewController alloc] init];
    
    rankVC.yuanchuang = self.yuanchuang.tag;
    //(@"^^^^^%ld", rankVC.yuanchuang);
    [self.navigationController pushViewController:rankVC animated:YES];
}
- (void)hotWordsRequest {
    [WZAFNetTool GetNetWithURL:@"http://s.search.bilibili.com/main/hotword?_device=android&_hwid=e647d7e136a2c2a9&appkey=c1b107428d337928&build=414000&platform=android&sign=25f2b98d8400c2e39f511f5c025139fd" WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        NSArray *array = reuslt[@"list"];
        for (NSDictionary *dic in array) {
            self.foundModel = [[CJFoundMainModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:self.foundModel];
        }
        
        [self.table reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotWordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotWordTableViewCellIdentifier"];
    self.foundModel = self.dataSource[indexPath.row];
    cell.foundModel = self.foundModel;
    cell.numberLabel.text = [NSString stringWithFormat:@" %ld:", indexPath.row + 1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    searchVC.foundModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:searchVC animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
